package cn.edu.ccnu.nlp;

import java.io.IOException;
import java.sql.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.HTablePool;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.RegexStringComparator;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.util.Bytes;

public class firstsql {
	
	public static Configuration configuration;
	static {
		configuration = HBaseConfiguration.create();
		//configuration.set("hbase.zookeeper.property.clientPort", "2181");
		//configuration.set("hbase.zookeeper.quorum", "192.168.1.100");
		//configuration.set("hbase.master", "192.168.1.100:600000");
	}
	
	/**
	 * @param args
	 */
	public static TreeMap QueryByDay(String tableName,String year,String month,String day,TreeMap map) {
		int sum = 0;
		int i=0;//remember number
		//String day_1 =null;
	 	String []day_1={"01","02","03","04","05","06","07","08","09","10","11","12","13",
	 		"14","15","16","17","18","19","20","21","22","23","00"};
		String []day_2 = new String[24];
	 	for(int j=0;j<24;j++){
	 		String hour=day_1[j];
	 		System.out.println("hour:"+hour);
			try {
				HTablePool pool = new HTablePool(configuration, 1000);
				HTable table = (HTable) pool.getTable(tableName);
				Filter filter = new SingleColumnValueFilter(Bytes
						.toBytes("date"), null, CompareOp.EQUAL, new RegexStringComparator(year+"-"+month+"-"+day+":"+hour+".*")); 
				Scan s = new Scan();
				s.setFilter(filter);
				ResultScanner rs = table.getScanner(s);
				for (Result r= rs.next();r!=null;r=rs.next()) {
								sum++;
		                  }
				    day_2[j]=hour;	
					map.put(day_2[j], sum);
					sum=0;
			} catch (Exception e) {
				e.printStackTrace();
 		}
	}
	return map;
}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		  TreeMap map=new TreeMap();
 		  firstsql di = new firstsql();
 //    	  di.QueryAll("news");
 		  TreeMap map2=new TreeMap();
 		  map2=di.QueryByDay("news", "2005", "01", "04",map);
		  System.out.println("the result of map:");
		  Iterator it = map2.entrySet().iterator();
		  while(it.hasNext())
		  {
			  Map.Entry  entry = (Map.Entry) it.next(); 
			  Object key=entry.getKey();
			  Object value=entry.getValue();
			  System.out.println("key="+key+" "+" value="+value); 
		  }
	}

}
